﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using System.Web;
using System.Web.SessionState;

namespace Acc_Web.svrs
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class VerifyCode : IHttpHandler, IRequiresSessionState 
    {
        #region IHttpHandler Members

        public void ProcessRequest(HttpContext context)
        {
            var strLen = context.Request["iLen"];
            var iLen = String.IsNullOrEmpty(strLen) ? 4 : Int32.Parse(strLen);
            var code = CreateRandomCode(iLen);
            HttpContext.Current.Session[typeof(VerifyCode).FullName]= code ;
            CreateImage(code, context);
        }

        public static Boolean CheckVerifyCode(String vfyCode)
        {
            var sessionStr = HttpContext.Current.Session[typeof (VerifyCode).FullName].ToString();
            return String.Compare(sessionStr, vfyCode, true)==0;
        }

        public bool IsReusable
        {
            get { return false; }
        }

        #endregion

        private static string CreateRandomCode(int codeCount)
        {
            const string allChar = "0,1,2,3,4,5,6,7,8,9";// ",A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
            var allCharArray = allChar.Split(',');
            var randomCode = "";
            var temp = -1;
            var rand = new Random();
            for (var i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i*temp*((int) DateTime.Now.Ticks));
                }
                var t = rand.Next(10);
                if (temp == t)
                {
                    return CreateRandomCode(codeCount);
                }
                temp = t;
                randomCode += allCharArray[t];
            }
            return randomCode;
        }


        private static void CreateImage(string checkCode, HttpContext context)
        {
            var iwidth = (int) (checkCode.Length*11.5);
            var image = new Bitmap(iwidth, 20);
            var g = Graphics.FromImage(image);
            try
            {
                var f = new Font("Arial", 10, FontStyle.Bold);
                var b = new SolidBrush(Color.White);
                g.FillRectangle(new SolidBrush(Color.Blue), 0, 0, image.Width, image.Height);
                g.Clear(Color.Blue);
                g.DrawString(checkCode, f, b, 3, 3);
                var blackPen = new Pen(Color.Black, 0);
                var rand = new Random();
                for (var i = 0; i < 5; i++)
                {
                    var y = rand.Next(image.Height);
                    g.DrawLine(blackPen, 0, y, image.Width, y);
                }

                context.Response.ClearContent();
                context.Response.ContentType = "image/Jpeg";
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}